/**
 * @Company:中享思途   
 * @Title:UserServiceImpl.java 
 * @Author:IL Clown   
 * @Date:2020年8月18日 上午9:24:24     
 */
package com.situ.shopsystem.service.Impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.situ.shopsystem.dao.UserDao;
import com.situ.shopsystem.pojo.LayuiResult;
import com.situ.shopsystem.pojo.User;
import com.situ.shopsystem.service.UserService;

/** 
 * @ClassName:UserServiceImpl 
 * @Description:(这里用一句话描述这个类的作用)  
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	/** 
	 * @Title: findUserByIdAndPass 
	 * @Description:后台使用 根据Id和Pass查找用户
	 * @param userId
	 * @param userPass
	 * @return  
	 */
	@Override
	public User findUserByIdAndPass(String userId, String userPass) {
		User user = userDao.findUserByIdAndPass(userId, userPass);
		return user;
	}

	/** 
	 * @Title: findAllManager 
	 * @Description:后台做显示用
	 * @return  
	 */
	@Override
	public LayuiResult findAllManager(Integer page, Integer limit, User user) {
		Integer first = limit * (page - 1);
		int count = userDao.findCount(2);
		if (user.getUserName() != null && user.getUserName() != "") {
			user.setUserName("%" + user.getUserName() + "%");
		}
		List<User> list = userDao.findAllManager(first, limit, user);
		LayuiResult layuiResult = new LayuiResult(0, null, count, list);

		return layuiResult;

	}

	/** 
	 * @Title: save 
	 * @Description:后台添加新用户
	 * @param user
	 * @param session
	 * @return  
	 */
	@Override
	public int save(User user, HttpSession session) {
		user.setUserLock(1);
		user.setActiveFlag(1);
		//根据session拿到注册的用户
		User us = (User) session.getAttribute("loginUser");
		user.setCreater(us.getRowId());
		user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));
		return userDao.save(user);
	}

	/** 
	 * @Title: update 
	 * @Description:后台使用 对用户信息作修改
	 * @param user
	 * @param session
	 * @return  
	 */
	@Override
	public int update(User user, HttpSession session) {
		//拿更新的用户Id
		User us = userDao.findById(user.getRowId());
		//更新用户姓名
		us.setUserName(user.getUserName());
		//用户账号
		us.setUserId(user.getUserId());
		//密码
		us.setUserPass(user.getUserPass());
		//电话
		us.setUserPhone(user.getUserPhone());
		User user2 = (User) session.getAttribute("loginUser");
		us.setUpdateBy(user2.getRowId());
		us.setUpdateTime(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));

		return userDao.update(us);
	}

	/** 
	 * @Title: findByUserName 
	 * @Description:通过id找user
	 * @param userName
	 * @return  
	 */
	@Override
	public int findByUserId(String userId) {

		User user = userDao.findByUserId(userId);
		if (user == null) {
			return 0;
		} else {
			return 1;
		}
	}

	/** 
	 * @Title: delete 
	 * @Description:用来做数据的删除 set activeflag的值为1
	 * @param rowId
	 * @return  
	 */
	@Override
	public int delete(Long rowId) {

		return userDao.delete(rowId);
	}

	/** 
	 * @Title: findByIdAndPass 
	 * @Description:前台做用户登录
	 * @param userId
	 * @param userPass
	 * @return  
	 */
	@Override
	public User findByIdAndPass(String userId, String userPass) {
		return userDao.findByIdAndPass(userId, userPass);
	}

	/** 
	 * @Title: saveUser 
	 * @Description:前台做用户注册
	 * @param user
	 * @param session
	 * @return  
	 */
	@Override
	public int saveUser(User user, HttpSession session) {
		user.setUserLock(1);
		user.setActiveFlag(1);
		user.setUserType(3);
		User us = (User) session.getAttribute("user");
		user.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh-mm-ss").format(new Date()));
		return userDao.save(user);
	}

}
